<?php

namespace App\Http\Controllers;

use App\Http\Resources\IndexResourceCollection;
use App\Http\Resources\SearchHotwordsResourceCollection;
use App\Http\Resources\IndexContentResourceCollection;
use App\Models\Module;
use App\Models\Country;
use App\Models\Article;

class ApiController extends Controller
{
    public function home(){
        return new IndexResourceCollection(Module::all());
    }

    public function hotwords(){
        $countries = Country::take(10)->get();
        $countrie_names = array();
        foreach ($countries as $country){
            array_push($countrie_names, $country->title);
        }
        $content = [
           'data' => $countrie_names,
            'msg' => 'success',
            'code' => 0
        ];
        return json_encode($content);
    }

    public function search($word){
        $country = Country::where('title', $word)->first();

        if($country){
            return new IndexContentResourceCollection($country->articles()->paginate());
        }

        return new IndexContentResourceCollection(Article::where('title', 'like', '%'.$word.'%')->paginate());
    }
}
